package cn.com.xinjinfu.pdoulcProduct.WebView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;
import cn.com.xinjinfu.pdoulcProduct.view.ProgressWebView;


/**
 * @Description:WebView界面，带自定义进度条显示
 * @author http://blog.csdn.net/finddreams
 */ 
public class BindWeiXinWeb extends Activity {

	protected ProgressWebView mWebView;
	private String url;
	private LoadStatusBox loadbox;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bing_wx_web);

		mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
		mWebView.getSettings().setJavaScriptEnabled(true);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		url = bundle.getString("url");
		loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
		loadbox.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				netCheck();
			}
		});
		loadbox.loading();
		findViewById(R.id.bind_wx).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				//把微信公众号复制到剪切板，然后跳转到微信
				TextViewUtil.copyText(BindWeiXinWeb.this, "pdoulc");
				ToastManager.show(BindWeiXinWeb.this, "已复制公众号");

				new Handler(new Handler.Callback() {
					@Override
					public boolean handleMessage(Message msg) {
						IWXAPI api = WXAPIFactory.createWXAPI(BindWeiXinWeb.this, "pdoulc");
						if (!api.openWXApp()) {					//判断是否安装微信客户端
							ToastManager.show(BindWeiXinWeb.this, "未安装微信客户端");
						}
						return true;
					}
				}).sendEmptyMessageDelayed(0, 1000);

			}
		});
	}

	private void netCheck(){
		if (!DataHandler.isNetworkConnected(this)) {
			loadbox.failed();
			ToastManager.show(this, "请检查网络连接");
			return;
		}else {
			initData();
			loadbox.success();
		}
	}

	protected void initData() {
		mWebView.loadUrl(url);
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
										String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				//这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
				L.d("errorCode=========="+errorCode);

			}
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				TitleManager.showTitle(BindWeiXinWeb.this, mWebView.getTitle(), "");
			}
		});

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView = null;

	}

}
